package RecordManagement;

import java.nio.ByteBuffer;

import PagedFile.Buffer;

/**
 * @author: Neil
 * @See PF_FileHead.
 */
public class RM_FileHead {
	public static final int RM_FILEHEAD_SIZE=16;
	public static final int RM_BMAPBITS = Buffer.BLOCK_SIZE - RM_FILEHEAD_SIZE;
	int numOfRecords;
	int recordSize;
	int recordPerPage;
	int firstRecordOffset;
	byte[] bitmap;
	static FunctionalClass.Bit_Operation bop = new FunctionalClass.Bit_Operation();
	
	public RM_FileHead(int nor, int rs, int rpp, int fro) {
		init_bitmap();
		this.numOfRecords = nor;
		this.recordSize = rs;
		this.recordPerPage = rpp;
		this.firstRecordOffset = fro;
	}
	
	public RM_FileHead(ByteBuffer bb) {
		init_bitmap();
		get_from_ByteBuffer(bb);
	}
	
	public String toString(){
		String ret = "(";
		ret += this.numOfRecords + ", ";
		ret += this.recordSize + ", ";
		ret += this.recordPerPage + ", ";
		ret += this.firstRecordOffset;
		ret += ")";
		return ret;
	}

	public void put_into_ByteBuffer(ByteBuffer bb) {
		bb.position(0);
		bb.putInt(this.numOfRecords);
		bb.putInt(this.recordSize);
		bb.putInt(this.recordPerPage);
		bb.putInt(this.firstRecordOffset);
		for (int i=0; i<RM_BMAPBITS; ++i)
			bb.put(bitmap[i]);
	}
	void init_bitmap(){
		bitmap = new byte[RM_BMAPBITS];
		bop.bitSet(bitmap, 0);	
		bop.bitSet(bitmap, 1);
	}
	
	public void get_from_ByteBuffer(ByteBuffer bb) {
		bb.position(0);
		this.numOfRecords = bb.getInt();
		this.recordSize = bb.getInt();
		this.recordPerPage = bb.getInt();
		this.firstRecordOffset = bb.getInt();
		for (int i=0; i<RM_BMAPBITS; ++i)
			bitmap[i] = bb.get();
	}
}




